﻿define([
    "dojo/_base/connect",
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/_base/Deferred",
    "dojo/dom-attr",

    "dijit/focus",
    "dijit/_CssStateMixin",
    "dijit/_Widget",
    "dijit/_TemplatedMixin",
    "dijit/_WidgetsInTemplateMixin",

// epi.cms
    "epi/cms/legacy/LegacyDialogPopup",
    "epi/cms/widget/_HasChildDialogMixin",
    "epi/epi",
    "epi/dependency",
    "epi/routes",
    "epi/string",
    "epi/shell/widget/_ValueRequiredMixin",

    "dojo/text!./Selector.html"
],
function (
    connect,
    declare,
    lang,
    Deferred,
    domAttr,

    focusManager,
    _CssStateMixin,
    _Widget,
    _TemplatedMixin,
    _WidgetsInTemplateMixin,

    LegacyDialogPopup,
    _HasChildDialogMixin,
    epi,
    dependency,
    routes,
    epiString,
    _ValueRequiredMixin,
    
    template
) {

    return declare("ow-epi-videocloud.Selector", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin, _CssStateMixin, _ValueRequiredMixin, _HasChildDialogMixin], {
        templateString: template,

        intermediateChanges: false,

        baseClass: 'imageExt',

        value: null,

        postMixInProperties: function () {
            this.inherited(arguments);            
        },

        postCreate: function () {
            this.inherited(arguments);
        },

        isValid: function () {
            // summary:
            //    Check if widget's value is valid.
            // tags:
            //    protected, override

            return true;
        },

        // Setter for value property
        _setValueAttr: function (value) {
            if (value) {
                this.videoId.value = value;
                this._set("value", value);
            }            
        },
        _setReadOnlyAttr: function (value) {
            if (value) {
                this._set("readOnly", value.item);
            }
        },

        _setStateAttr: function (value) {
            if (value) {
                this._set("state", value.item);
            }
        },

        // Setter for intermediateChanges
        _setIntermediateChangesAttr: function (value) {
            if (value) {
                this._set("intermediateChanges", value);
            }
        },
        destroy: function () {
            if (this._dialog) {
                this._dialog.destroyRecursive();
                delete this._dialog;
            }

            this.inherited(arguments);
        },
        _onDialogCallback: function (value) {
            // cancel
            if (!value) {
                return;
            }

            if (value.closeAction === "insert" && value.item) {
                this.set("value", value.item);
            } else if (value.closeAction == "clear") {
                this.set("value", null);
            }

            this.onChange(value.item);
        },
        onChange: function (value) {
            // Event
        },
        _onDialogHide: function () {
            this.isShowingChildDialog = false;

            this._dialog.destroyRecursive();
            delete this._dialog;

            focusManager.focus(this.videoId);
        },

        _selectVideo: function () {
            if (this.readOnly) {
                return;
            }

            this.onFocus();

            var deferred = this._createImageDialogInstance();
            if (deferred) {
                Deferred.when(deferred, lang.hitch(this, function () {
                    this._shoDialog();
                }));
            } else {
                this._shoDialog();
            }
        },

        _shoDialog: function () {
            this.connect(this._dialog, "onCallback", this._onDialogCallback);
            this.connect(this._dialog, 'onHide', this._onDialogHide);
            this.isShowingChildDialog = true;
            this._dialog.show();
        },

        _createImageDialogInstance: function() {
            if (this._dialog) {
                return null;
            }
            
            var imageDialogClassName = this.metadata.customEditorSettings.imageDialogClassName;
            var imageDialogSettings = this.metadata.customEditorSettings.imageDialogSettings;
            if (this.value) {
                imageDialogSettings.selectedVideoId = this.value;
            }

            var def = new Deferred();
            var widgetType = epiString.slashName(imageDialogClassName);
            require([widgetType], lang.hitch(this, function(imageDialogType) {
                this._imageDialogType = imageDialogType;
                def.resolve();
            }));

            Deferred.when(def, lang.hitch(this, function() {
                this._dialog = new this._imageDialogType(imageDialogSettings);
            }));
            
            return def;
        }
    });
});